﻿using Api.Application.Command.Leave;
using Api.DTO;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Leave
{
    public class GetLeaveHandler : IRequestHandler<GetLeaveCommand, ApiPaging<LeaveDTO>>
    {
        private readonly IBase<LeaveModel> leaveRepo;
        private readonly IBase<DeionModel> deionRepo;
        private readonly IBase<UserModel> userRepo;
        private readonly IBase<UserRoleModel> userRoleRepo;
        private readonly IBase<RoleModel> roleRepo;

        public GetLeaveHandler(IBase<LeaveModel> leaveRepo,IBase<DeionModel> deionRepo,IBase<UserModel> userRepo,IBase<UserRoleModel> userRoleRepo,IBase<RoleModel> roleRepo)
        {
            this.leaveRepo = leaveRepo;
            this.deionRepo = deionRepo;
            this.userRepo = userRepo;
            this.userRoleRepo = userRoleRepo;
            this.roleRepo = roleRepo;
        }
        public Task<ApiPaging<LeaveDTO>> Handle(GetLeaveCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<LeaveDTO> paging = new ApiPaging<LeaveDTO>();
            var leave = leaveRepo.GetAll().Where(x => x.IsDeleted == false);
            var deion = deionRepo.GetAll();
            
            
            if(!string.IsNullOrEmpty(request.UserName))
            {
                leave = leave.Where(x => x.UserName.Contains(request.UserName));
            }
            if(request.ApprovalState!=null)
            {
                leave = leave.Where(x => x.ApprovalState == request.ApprovalState);
            }
            if (request.LeaveStartTime != null)
            {
                leave = leave.Where(x => x.LeaveStartTime >= request.LeaveStartTime);
            }
            if (request.LeaveEndTime != null)
            {
                leave = leave.Where(x => x.LeaveEndTime <= request.LeaveEndTime);
            }

            string? RoleName = null;
            int? DeionId=null;
            if(!string.IsNullOrEmpty(request.CurrentUser))
            {
                var user = userRepo.GetAll().FirstOrDefault(x=>x.UserName==request.CurrentUser);
                if (user != null)
                {
                    DeionId=user.DeionId;
                    var userrole = userRoleRepo.GetAll().FirstOrDefault(x => x.UserId == user.Id);
                    if (userrole != null)
                    {
                        var role = roleRepo.GetAll().FirstOrDefault(x=>x.Id==userrole.RoleId);
                        if (role != null)
                        {
                            RoleName = role.RoleName;
                        }

                    }
                }
            }
            if (RoleName == "董事长")
            {

            }
            else if (RoleName == "总经理" && DeionId.HasValue)
            {
                var deions = userRepo.GetAll().Where(x => x.DeionId == DeionId.Value).Select(x => x.UserName).ToList();
                leave = leave.Where(x => deions.Contains(x.UserName));
            }
            else
            {
                if (!string.IsNullOrEmpty(request.CurrentUser))
                {
                    leave = leave.Where(x => x.UserName == request.CurrentUser);
                }
            }

            

            var linq = from a in leave
                       join b in deion
                       on a.DeionId equals b.DeionId
                       select new LeaveDTO()
                       {
                           Id = a.Id,
                           DeionId = a.DeionId,
                           ApprocalYuan=a.ApprocalYuan,
                           ApprovalState=a.ApprovalState,  
                           BaoDaoTime=a.BaoDaoTime,
                           BuFuName=a.BuFuName,
                           LeaveEndTime=a.LeaveEndTime,
                           LeaveReason=a.LeaveReason,
                           LeaveStartTime=a.LeaveStartTime,
                           LeaveType=a.LeaveType,
                           UserName=a.UserName,
                           DeionName=b.DeionName,
                           CrearTime=a.CrearTime,
                           LeaveZong=a.LeaveZong,
                       };
            paging.TotalCount=linq.Count();
            paging.Data = linq.OrderByDescending(x=>x.CrearTime).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            paging.TotalPage = (int)Math.Ceiling((paging.TotalCount*1.0)/request.Size);
            paging.Code = ApiEnum.OK;
            paging.Msg = "请假审批列表显示成功";
            return Task.FromResult(paging);
        }
    }
}
